package ee.ut.cs.mc.madp.p2pdivider;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;
import ee.ut.cs.mc.madp.p2pdivider.fragment.DevicesFragment;
import ee.ut.cs.mc.madp.p2pdivider.fragment.MediaFragment;
import ee.ut.cs.mc.madp.p2pdivider.fragment.PlayFragment;
import ee.ut.cs.mc.madp.p2pdivider.fragment.UIFragment;

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
 * sections of the app.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public static final String ARG_SECTION_NUMBER = "section_number";

    private final Context adapterContext;
    private final SparseArray<Fragment> fragments = new SparseArray<Fragment>(3);

    public SectionsPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        adapterContext = context;
    }

    @Override
    public Fragment getItem(int i) {
        switch (UIFragment.getFragment(i)) {
        case DEVICES:
            return setupDevices(i);
        case MEDIA:
            return setupMedia(i);
        case PLAY:
            return setupPlay(i);
        }

        throw new RuntimeException("Unknown item: " + i);
    }

    private Fragment setupDevices(int i) {
        Fragment fragment = fragments.get(i);
        if (fragment == null) {
            fragment = new DevicesFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            fragments.put(i, fragment);
        }

        return fragment;
    }

    private Fragment setupMedia(int i) {
        Fragment fragment = fragments.get(i);
        if (fragment == null) {
            fragment = new MediaFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            fragments.put(i, fragment);
        }
        return fragment;
    }

    private Fragment setupPlay(int i) {
        Fragment fragment = fragments.get(i);
        if (fragment == null) {
            fragment = new PlayFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            fragments.put(i, fragment);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return adapterContext.getString(R.string.tab_devices).toUpperCase();
        case 1:
            return adapterContext.getString(R.string.tab_media).toUpperCase();
        case 2:
            return adapterContext.getString(R.string.tab_play).toUpperCase();
        }
        return null;
    }
}